
import { _decorator, Component, Node, Vec3, Vec2, Sprite, SpriteFrame } from 'cc';
import { FieldData } from './FieldData';
import { FogEffect } from './FogEffect';
import { FogMap } from './FogMap';
import { FogRenderer } from './FogRenderer';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Explorer
 * DateTime = Fri Aug 12 2022 17:13:42 GMT+0800 (中国标准时间)
 * Author = linruimin
 * FileBasename = Explorer.ts
 * FileBasenameNoExtension = Explorer
 * URL = db://assets/script/Explorer.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('Explorer')
export class Explorer extends Component {

    radius = 5;

    mapPosition = Vec2.ZERO;

    fieldData : FieldData = null!;
    @property(Sprite)
    fogWar : Sprite = null!;

    mixTime = 0;
    refreshTime = 0;
    kDispearSpeed = 20;
    kRefreshTextureSpeed = 20;
    isFieldDataUpdated = false;

    fogRenderer : FogRenderer = null!;
    fogMap : FogMap = null!;

    start () {
        this.fieldData = new FieldData(this.node.position, this.radius);
        this.fogRenderer = new FogRenderer();
        this.fogMap = new FogMap();

        this.fogWar.customMaterial?.setProperty('fowTexture', this.fogMap.getMaskTexture());
    }

    update (deltaTime: number) 
    {
        let pos = this.fogMap.getPosition(this.node.position);

        this.mapPosition = pos;
        this.fieldData.position = this.node.position;

        this.updateFieldData(this.fieldData);

        this.updateFog(deltaTime);
    }

    updateFieldData(fieldData : FieldData)
    {
        if (!this.isFieldDataUpdated)
        {
            this.fogMap.setVisiable(fieldData);
            
            this.isFieldDataUpdated = true;
        }
    }

    updateFog(deltaTime : number)
    {
        if (this.mixTime >= 1.0)
        {
            if (this.refreshTime >= 1.0)
            {
                this.refreshTime = 0;
                if (this.fogMap.refreshTexture(this.fieldData))
                {
                    this.setFogFade(0);
                    this.mixTime = 0;
                    this.isFieldDataUpdated = false;
                }
            }
            else
            {
                this.refreshTime += deltaTime * this.kRefreshTextureSpeed;
            }
        }
        else
        {
            this.mixTime += deltaTime * this.kDispearSpeed;
            this.setFogFade(this.mixTime);
        }
    }

    setFogFade(fade : number)
    {
        this.fogWar.customMaterial?.setProperty('mixValue', fade);
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
